package model;

import java.util.ArrayList;
import java.util.List;

public class TableModel {
    public ArrayList<CardStack> data;
    public TableModel() {
    	
    	// list of all stacks on table
        data = new ArrayList<CardStack>();
        init();
    }
    private void init() {
        CardStack stack = new CardStack(225, 225);
        for(int n=1; n<=13; n++) {
            stack.push(new Card(n, Suit.SPADE));
            stack.push(new Card(n, Suit.DIAMOND));
            stack.push(new Card(n, Suit.HEART));
            stack.push(new Card(n, Suit.CLUB));
        }
        data.add(stack);
    }
        
    public void cleanup()
    {
    	boolean[] empties = new boolean[data.size()];
    	for (int i = 0; i < data.size(); i++)
    	{
    		CardStack stack = data.get(i);
    		if (stack.isEmpty())
    			empties[i] = true;
    	}
    	
    	for (int j = 0; j < data.size(); j++)
    	{
    		if (empties[j])
    			data.remove(j);
    	}
    	
    }
    
    public boolean moveStack(int stackIndex, int newX, int newY)
    {
    	CardStack stack = data.get(stackIndex);
    	stack.x = newX;
    	stack.y = newY;
    	
    	return true;
    }
    
    public void unhighlightAllStacks()
    {
    	cleanup();
  
    	for (CardStack s : data)
    	{
    		s.checkRep();
    		s.unhighlightStack();
    	}
    	
    }
    
    public CardStack moveTopFromStack(CardStack oldStack, int newX, int newY)
    {
    	CardStack newStack = new CardStack(newX, newY);
    	
    	Card top = oldStack.pop();
    	newStack.push(top);
    	
    	data.add(newStack);
    	
    	return newStack;
    	
    }
    
    public boolean moveTopFromStack(int stackIndex, int newX, int newY)
    {
    	CardStack oldStack = data.get(stackIndex);
    	CardStack newStack = new CardStack(newX, newY);
    	
    	Card top = oldStack.pop();
    	newStack.push(top);
    	
    	data.add(newStack);
    	
    	return true;
    }
    
    public boolean moveTopManyFromStack(int stackIndex, int depth, int newX, int newY)
    {
    	CardStack oldStack = data.get(stackIndex);
    	CardStack newStack = new CardStack(newX, newY);
    	
    	for (int i = 0; i < depth; i++)
    	{
    		newStack.push(oldStack.pop());    		
    	}
    	
    	data.add(newStack);
    	
    	return true;
    }
    
    
    /**
     * add all cards in stack A to stack B. remove stack A from data list
     * @param stackA
     * @param stackB
     * @return combined stack
     */
    public CardStack combine(CardStack stackA, CardStack stackB)
    {
    	stackB.addAll(stackA);
    	data.remove(stackA);
    	return stackB;
    }
        
}